package tk.mybatis.web.webSocket;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
//该类针对每个客户端连接都各创建一个实例
@ServerEndpoint("/test1-websocket")
public class Test1WebSocket {

    private double random;
    private Session session;

    public Test1WebSocket() {
        random = Math.random();
    }


    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        Test1WebSocketPool.addConnection(this, "");
    }
    @OnClose
    public void onClose() {
        Test1WebSocketPool.removeConnection(this);
    }
    @OnMessage
    public void onMessage(String message, Session session) {
        Test1WebSocketPool.sendMessage("回复" + message);
    }
    @OnError
    public void onError(Session session, Throwable error) {
        System.out.println("发生错误");
        error.printStackTrace();
    }
    public void send(String message) {
        this.session.getAsyncRemote().sendText(message);
    }
}
